001    /* $RCSfile: IntegerUtil.java,v $
002     * $Revision: 1.3 $
003     * $Date: 2002/01/04 14:05:31 $
004     * $Author: uwe $
005     * $State: Exp $
006     *
007     * Created on July 12, 2001, 5:20 PM
008     *
009     * Copyright (C) 2001 Uwe Guenther <uwe@cscc.de>
010     *
011     * This file is part of the jhbci JCE-ServiceProvider. The jhbci JCE-
012     * ServiceProvider is a library, written in JavaTM, that should be 
013     * used in HBCI banking applications (clients and may be servers),
014     * to do cryptographic operations.
015     *
016     * The jhbci library is free software; you can redistribute it and/or
017     * modify it under the terms of the GNU Lesser General Public
018     * License as published by the Free Software Foundation; either
019     * version 2.1 of the License, or (at your option) any later version.
020     *
021     * The jhbci library is distributed in the hope that it will be useful,
022     * but WITHOUT ANY WARRANTY; without even the implied warranty of
023     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
024     * Lesser General Public License for more details.
025     *
026     * You should have received a copy of the GNU Lesser General Public
027     * License along with this library; if not, write to the Free Software
028     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
029     *
030     */
031    
032    package de.cscc.crypto.util;
033    
034    /** 
035     * IntegerUtil Class.
036     *
037     * Class that hold only static methods to convert ints and int arrays to
038     * Strings. These Strings holds the hex or binary representation of the
039     * converted ints or int arrays. This class can't be instance, because
040     * its constructor is private.
041     *
042     * @author  <a href=mailto:uwe@cscc.de>Uwe Günther</a>
043     * @version $Revision: 1.3 $
044     */
045    public final class IntegerUtil {
046        
047        /** This class can't be instance, because its constructor is private. */
048        private IntegerUtil() {}
049    
050        /** 
051         * Converts byte to binary representation.
052         *
053         * @param in int that will be converted.
054         * @return String with hex representation of in.
055         */        
056        public static String toBin(int in){
057            
058            return toBinGeneric(in);
059        }
060        
061        /** 
062         * Converts int[] to binary representation.
063         *
064         * @param in int[] that will be converted.
065         * @return String with hex representation of in.
066         */        
067        public static String toBin(int[] in){
068            
069            StringBuffer tmp = new StringBuffer();
070            
071            for (int i = 0; i < in.length; i++) {
072                tmp.append(toBin(in[i]) + " ");
073            }
074              
075            return "[ " + tmp + "]";
076        
077        }
078        
079        /** 
080         * Converts int to binary representation.
081         *
082         * @param in int that will be converted
083         * @return String with hex representation of in
084         */        
085        private static String toBinGeneric(int in){
086    
087            StringBuffer tmp = new StringBuffer();
088            
089            for(int i = 0; i < 32; i++){
090                tmp.insert(0, (in >>> i) & 0x00000001);
091                
092            }
093            
094            return "0b" + tmp.toString();
095        }
096        
097        /** 
098         * Converts int to hex representation.
099         *
100         * @param in int that will be converted.
101         * @return String with hex representation of in.
102         */        
103        public static String toHex(int in) {
104            
105            return toHexGeneric(in);
106        }
107    
108        /**
109         * Converts int[] to hex representation.
110         *
111         * @param in int[] that will be converted.
112         * @return String with hex representation of in.
113         */        
114        public static String toHex(int[] in) {
115            
116            StringBuffer tmp = new StringBuffer();
117            
118            for (int i = 0; i < in.length; i++) {
119                tmp.append(toHexGeneric(in[i]) + " ");
120            }
121            
122            return "[ " + tmp.toString() + "]";
123        }
124        
125        /**
126         * Converts int to binary representation.
127         *
128         * @param in int that will be converted
129         * @return String with hex representation of in
130         */        
131        private static String toHexGeneric(int in){
132            return ByteUtil.toHex(toByteArray(in));     
133        }
134        
135        /**
136         * Converts int to byte[4]
137         *
138         * @param in int that should be converted to byte[4].
139         * @return byte array that holds the int in.
140         */    
141        public static byte[] toByteArray(int in) {
142            
143            byte[] temp = new byte[4];
144            
145            temp[0] = (byte)((in >>> 24) & 0xff);
146            temp[1] = (byte)((in >>> 16) & 0xff);
147            temp[2] = (byte)((in >>>  8) & 0xff);
148            temp[3] = (byte)((in >>>  0) & 0xff);
149            
150            return temp;
151        }
152    }